package com.order.itemcategoryproject.controller;

import com.order.itemcategoryproject.pojo.dArcStaff;
import com.order.itemcategoryproject.service.dArcStaffService;
import com.order.itemcategoryproject.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.order.itemcategoryproject.utils.ParseUrl.getUrl;

@RestController
@RequestMapping("/staff")
public class dArcStaffController {



    @Autowired
    private dArcStaffService dArcStaffService;


    /**
     * 通过路劲参数获取登陆人编号
     * @param request
     * @return
     */
    @GetMapping("/selectStaffByStaffId")
    public AjaxResult getStaffInfoByStaffId(HttpServletRequest request){
        // 1、获取路径参数
        String queryString = request.getQueryString();
        String staffID = getUrl(queryString, "StaffID");

        // 首先判断制单人编号是否不为空
        if (StringUtils.isBlank(staffID)) {
            // 如果为空 那么就返回
            return AjaxResult.error("参数未被定义!!!!!!!");
        }
        // 根据路径获取到的制单人编号获取制单人对象
        dArcStaff staffInfoByStaId = dArcStaffService.getStaffInfoByStaId(staffID);
        if (ObjectUtils.isEmpty(staffInfoByStaId)) {
            return AjaxResult.error("未获取到当前对象!!!!!!");
        }

        return AjaxResult.success(staffInfoByStaId);
    }

    /**
     * 根据是客户编号以及密码获取到当前对象是否存在
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/getStaffInfoByIdAndPwd/{username}/{password}")
    public AjaxResult getStaffInfoByIdAndPassword(@PathVariable String username, @PathVariable String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isBlank(password)) {
            return AjaxResult.error("The parameters are not defined");
        }

        dArcStaff dArcStaff = dArcStaffService.selectStaffInfoByIdAndPassword(username, password);

        if (ObjectUtils.isEmpty(dArcStaff)) {
            return AjaxResult.error("Invalid username or password.");
        }

        if (username.equals(dArcStaff.getStaffId()) && password.equals(dArcStaff.getNew_password())) {
            return AjaxResult.success(dArcStaff);
        } else {
            return AjaxResult.error("Please check your username and password.");
        }
    }
}
